<?php

/*
 *  controller  *
*/
	echo "controller.php\n";
	$cgen = "";
	$cgen .= pr("<?php");
	$cgen .= pr("");
	$cgen .= pr("class ".$strUP."Controller extends AppController {");
	$cgen .= pr("\tvar \$name = '".$strUP."';");
	$cgen .= pr("\tvar \$components = array ('Pagination','Acl');");
	$cgen .= pr("\tvar \$helpers = array('Html','Form','Accessibility','Pagination');");
	$cgen .= pr("");
	$cgen .= pr("\tfunction index() {");
	$cgen .= pr("\t\t\$this->set('title', '".$strUP."');");
	$cgen .= pr("\t\t\$".$strDP." = \$this->".$strUS."->findAll();");
	$cgen .= pr("\t\t\$this->set('".$strDP."', \$".$strDP.");");
	$cgen .= pr("\t}");
	$cgen .= pr("");
	$cgen .= pr("\tfunction view( \$id=null ) {");
	$cgen .= pr("\t\tif (!\$id) \$this->redirect('/".$strDP."');");
	$cgen .= pr("\t\t\$this->set('title','Visualizar ".$strDS."');");
	$cgen .= pr("\t\t\$this->".$strUS."->id = \$id;");
	$cgen .= pr("\t\t\$this->set('".$strDS."', \$this->".$strUS."->read());");
	$cgen .= pr("\t}");
	$cgen .= pr("");
	$cgen .= pr("\tfunction add() {");
	$cgen .= pr("\t\t\$this->set('title','Cadastrar ".$strDS."');");
	foreach ($campos as $campo) {
		if (substr($campo,-3,3) == "_id") {
			$col_name = substr($campo,0,strlen($campo)-3);
			$cgen .= pr("\t\t\$this->set('".plural($col_name)."', \$this->".$strUS."->".ucfirst($col_name)."->generateList(null, null, null, '{n}.".ucfirst($col_name).".id', '{n}.".ucfirst($col_name).".nome'));");
		}
	}
	$cgen .= pr("\t\tif (!empty(\$this->data)) {");
	$cgen .= pr("\t\t\t\$aco = new Aco();");
	$cgen .= pr("\t\t\t\$aco->create(\$this->".$strUS."->id, null, \$this->data['".$strUS."']['nome']);");
	$cgen .= pr("\t\t\tif (\$this->".$strUS."->save(\$this->data)) { ");
	if (substr($strDS,-1,1)== 'a' || substr($strDS,-1,1)== 'e')
		$art = 'a';
	else
		$art = 'o';
	$cgen .= pr("\t\t\t\t\$_SESSION['mensagem']['success'][] = '".$strUS." cadastrad".$art." com sucesso!';");
	$cgen .= pr("\t\t\t} else {");
	$cgen .= pr("\t\t\t\t\$_SESSION['mensagem']['error'][] = 'Erro ao tentar cadastrar ".$art." ".$strDS.".';");
	$cgen .= pr("\t\t\t}");
	$cgen .= pr("\t\t\t\$this->redirect('/".$strDP."');");
	$cgen .= pr("\t\t}");
	$cgen .= pr("\t}");
	$cgen .= pr("");
	$cgen .= pr("\tfunction edit( \$id=null) {");
	$cgen .= pr("\t\tif (!\$id) \$this->redirect('/".$strDP."');");
	$cgen .= pr("\t\t\$this->set('title','Editar ".$strDS."');");
	foreach ($campos as $campo) {
		if (substr($campo,-3,3) == "_id") {
			$col_name = substr($campo,0,strlen($campo)-3);
			$cgen .= pr("\t\t\$this->set('".plural($col_name)."', \$this->".$strUS."->".ucfirst($col_name)."->generateList(null, null, null, '{n}.".ucfirst($col_name).".id', '{n}.".ucfirst($col_name).".nome'));");
		}
	}
	$cgen .= pr("\t\t\$this->".$strUS."->id = \$id;");
	$cgen .= pr("\t\tif (empty(\$this->data)) {");
	$cgen .= pr("\t\t\t\$this->data = \$this->".$strUS."->read();");
	$cgen .= pr("\t\t} else {");
	$cgen .= pr("\t\t\tif (\$this->".$strUS."->save(\$this->data['".$strUS."'])) {");
	$cgen .= pr("\t\t\t\t\$_SESSION['mensagem']['success'][] = '".$strUS." alterad".$art." com sucesso!';");
	$cgen .= pr("\t\t\t} else {");
	$cgen .= pr("\t\t\t\t\$_SESSION['mensagem']['error'][] = 'Erro ao tentar alterar ".$art." ".$strDS.".';");
	$cgen .= pr("\t\t\t}");
	$cgen .= pr("\t\t\t\$this->redirect('/".$strDP."');");
	$cgen .= pr("\t\t}");
	$cgen .= pr("\t\t\$".$strDS." = \$this->".$strUS."->find(array('`".$strUS."`.id' => \$id));");
	$cgen .= pr("\t\t\$this->set('".$strDS."', \$".$strDS.");");
	$cgen .= pr("\t}");
	$cgen .= pr("");
	$cgen .= pr("\tfunction delete(\$id) {");
	$cgen .= pr("\t\tif (!\$id) \$this->redirect('/".$strDP."');");
	$cgen .= pr("\t\tif (\$this->".$strUS."->del(\$id)) {");
	$cgen .= pr("\t\t\t\$_SESSION['mensagem']['success'][] = '".$strUS." excluid".$art." com sucesso!';");
	$cgen .= pr("\t\t} else {");
	$cgen .= pr("\t\t\t\$_SESSION['mensagem']['error'][] = 'Erro ao tentar excluir ".$art." ".$strDS.".';");
	$cgen .= pr("\t\t}");
	$cgen .= pr("\t\t\$this->redirect('/".$strDP."');");
	$cgen .= pr("\t}");
	$cgen .= pr("}");
	$cgen .= pr("?>");
	echo $cgen;
	
	
	if ($_GET['submit'] == 'index') {
		$furl = "../cake/app/views/".$strDP."/";
		if (file_exists ($furl) == false)
			mkdir($furl);
		$furl .= "index.thtml";
		$f = fopen($furl,"w");
		fwrite($f,html_entity_decode($igen));
		fclose($f);
	}
	if ($_GET['submit'] == 'edit') {
		$furl = "../cake/app/views/".$strDP."/";
		if (file_exists ($furl) == false)
			mkdir($furl);
		$furl .= "edit.thtml";
		$f = fopen($furl,"w");
		fwrite($f,html_entity_decode($egen));
		fclose($f);
	}
	if ($_GET['submit'] == 'view') {
		$furl = "../cake/app/views/".$strDP."/";
		if (file_exists ($furl) == false)
			mkdir($furl);
		$furl .= "view.thtml";
		$f = fopen($furl,"w");
		fwrite($f,html_entity_decode($vgen));
		fclose($f);
	}
	if ($_GET['submit'] == 'add') {
		$furl = "../cake/app/views/".$strDP."/";
		if (file_exists ($furl) == false)
			mkdir($furl);
		$furl .= "add.thtml";
		$f = fopen($furl,"w");
		fwrite($f,html_entity_decode($agen));
		fclose($f);
	}
	if ($_GET['submit'] == 'controller') {
		$furl = "../cake/app/controllers/".$strDP."_controller.php";
		$f = fopen($furl,"w");
		fwrite($f,html_entity_decode($cgen));
		fclose($f);
	}

?>